using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ToDoListWeb.Service;
using ToDoListWeb.Models;
using Microsoft.AspNetCore.Http;

namespace ToDoListWeb.Controllers
{
    public class TypeController : Controller
    {
        TypeService _typeService = new TypeService();
        UserService _userService = new UserService();

        /// <summary>
        /// 删除类型及类型下所有清单、事项
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public IActionResult DeleteType(int typeId)
        {
            _typeService.DeleteType(typeId);
            _userService.UpdateUserModel(GetUser());
            return View(GetUser());
        }
        /// <summary>
        /// 编辑类型（方法）
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public IActionResult CompileType(int typeId, string typeName)
        {
            if (typeId == 0 || typeName == null)
                View("ShowEvents", GetUser());
            _typeService.ChangeTypeName(typeId, typeName);
            _userService.UpdateUserModel(GetUser());
            return View("ShowEvents", GetUser());
        }
        /// <summary>
        /// 编辑类型（视图）
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public IActionResult CompileTypeView(int typeId)
        {
            var type = _typeService.GetType(typeId);
            return View(type);
        }

        /// <summary>
        /// 添加清单类型
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddTypeView()
        {
            _userService.UpdateUserModel(GetUser());
            return View(GetUser());
        }

        //[HttpGet]
        //public IActionResult FoundType()
        //{
        //    return View();
        //}
        /// <summary>
        /// 添加清单类型
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult FoundType(string typeName)
        {
            if (typeName != null)
            {
                _typeService.CreateType(GetUser(), typeName);
                _userService.UpdateUserModel(GetUser());
            }
            return View(GetUser());
        }

        UserModel GetUser()
        {
            int userId = (int)HttpContext.Session.GetInt32("userId");
            return _userService.GetUserModel(userId);
        }
    }
}